package cn.edu.sgu.www.mhxysy.service.xingyin.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.xingyin.XingyinCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.xingyin.XingyinCategory;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.xingyin.XingyinCategoryPager;
import cn.edu.sgu.www.mhxysy.service.xingyin.XingyinCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class XingyinCategoryServiceImpl implements XingyinCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final XingyinCategoryMapper xingyinCategoryMapper;

    @Autowired
    public XingyinCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            XingyinCategoryMapper xingyinCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.xingyinCategoryMapper = xingyinCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_XINGYIN;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(XingyinCategoryDTO xingyinCategoryDTO) {
        XingyinCategory xingyinCategory = toEntity(xingyinCategoryDTO);

        xingyinCategory.setId(null);

        xingyinCategoryMapper.insert(xingyinCategory);
    }

    @Override
    public void updateById(XingyinCategoryDTO xingyinCategoryDTO) {
        ObjectUtils.requireNonNull(xingyinCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        XingyinCategory xingyinCategory = toEntity(xingyinCategoryDTO);

        xingyinCategoryMapper.updateById(xingyinCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<XingyinCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                xingyinCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<XingyinCategory> selectAll() {
        return xingyinCategoryMapper.selectList(null);
    }

    @Override
    public XingyinCategory selectById(Integer id) {
        return xingyinCategoryMapper.selectById(id);
    }

    @Override
    public List<XingyinCategory> selectByType(Integer type) {
        QueryWrapper<XingyinCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(type != null, "type", type);

        return xingyinCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<XingyinCategory> selectByPage(XingyinCategoryPager pager) {
        QueryWrapper<XingyinCategory> queryWrapper = new QueryWrapper<>();
        Page<XingyinCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );

        return xingyinCategoryMapper.selectPage(page, queryWrapper);
    }

    private XingyinCategory toEntity(XingyinCategoryDTO xingyinCategoryDTO) {
        XingyinCategory xingyinCategory = new XingyinCategory();

        xingyinCategory.setId(xingyinCategoryDTO.getId());
        xingyinCategory.setName(xingyinCategoryDTO.getName());
        xingyinCategory.setType(xingyinCategoryDTO.getType());
        xingyinCategory.setGrade(xingyinCategoryDTO.getGrade());

        return xingyinCategory;
    }

}